package study.wechat.pub.client.menu;

import cn.hutool.core.io.resource.ResourceUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import study.wechat.pub.client.WechatBody;
import study.wechat.pub.client.WechatHttp;

import java.util.Map;

/**
 * https://developers.weixin.qq.com/doc/offiaccount/Getting_Started/Overview.html
 */
@Slf4j
@Component
public class MenuClient {

    private String PATH = "/cgi-bin/menu/create";

    public WechatBody createMenu(String accessToken) {
        //
        String json = ResourceUtil.readUtf8Str("wechat/menu.json");
        MenuInfo menuInfo = JSONUtil.toBean(json, MenuInfo.class);
        //
        Map<String, Object> querys = MapUtil.newHashMap();
        querys.put("access_token", accessToken);
        //
        String body = WechatHttp.post(PATH, JSONUtil.toJsonStr(menuInfo), querys);
        WechatBody info = JSONUtil.toBean(body, WechatBody.class);
        return info;
    }

    public static void main(String[] args) {
        String json = ResourceUtil.readUtf8Str("wechat/menu.json");
        MenuInfo menuInfo = JSONUtil.toBean(json, MenuInfo.class);
        menuInfo.getButton().forEach(e -> {
            log.info("{}", e.getName());
        });
    }
}
